﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CocaCola_Inventory_System.Forms
{
    public partial class RegisterUser : Form
    {
        public RegisterUser()
        {
            InitializeComponent();
        }

        private void btn_register_Click(object sender, EventArgs e)
        {
            if (!validate_data())
            {
                MessageBox.Show(null, "Data validation failed, please check required fields' syntax", "Validation Failed", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }

            if (Data_Access.registerUserDB.registerNewUser(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con,txt_loginId.Text, txt_name.Text, txt_title.Text, txt_email.Text, txt_telephone.Text, txt_mobile.Text, txt_ExtensionAttr.Text, txt_department.Text, txt_departmentNo.Text, txt_postalAddress.Text, txt_postalCode.Text, txt_city.Text, txt_country.Text, txt_homeDir.Text, txt_homeDrive.Text, txt_description.Text))
                MessageBox.Show(null, "User registered successfully.", "Registration Complete", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            else
                MessageBox.Show(null, "User registration failed", "Registration Failed", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }


        /// <summary>
        /// Validates the data and returns true if successful
        /// </summary>
        /// <returns>Boolean</returns>
        private bool validate_data()
        {
            if (txt_loginId.Text.Length == 0)
                return false;
            else if (txt_name.Text.Length == 0)
                return false;

            
            //Some validation options (if needed)
            //try
            //{
            //    string val_pattern = "[A-Za-z0-9@ ]";
            //    using (System.Text.RegularExpressions.Regex regValidate = new System.Text.RegularExpressions.Regex(val_pattern))
            //    {
            //        if (!regValidate.IsMatch(txt_loginId.Text))
            //            throw new ValidationException("Check LoginId syntax");
            //        else if (!regValidate.IsMatch(txt_name.Text))
            //            throw new ValidationException("Check name syntax");
            //        else if (!regValidate.IsMatch(txt_departmentNo.Text))
            //            throw new ValidationException("Check department syntax");
            //        else if (!regValidate.IsMatch(txt_department.Text))
            //            throw new ValidationException("Check department syntax");
            //        else if (!regValidate.IsMatch(txt_email.Text))
            //            throw new ValidationException("Check email syntax");
            //        else if (!regValidate.IsMatch(txt_postalCode.Text))
            //            throw new ValidationException("Check postal code syntax");
            //        else if (!regValidate.IsMatch(txt_postalAddress.Text))
            //            throw new ValidationException("Check postal address syntax");
            //        else if (!regValidate.IsMatch(txt_title.Text))
            //            throw new ValidationException("Check title syntax");
            //        else if (!regValidate.IsMatch(txt_mobile.Text))
            //            throw new ValidationException("Check mobile syntax");
            //        else if (!regValidate.IsMatch(txt_telephone.Text))
            //            throw new ValidationException("Check telephone syntax");
            //        else if (!regValidate.IsMatch(txt_ExtensionAttr.Text))
            //            throw new ValidationException("Check extension attribute syntax");
            //        else if (!regValidate.IsMatch(txt_homeDrive.Text))
            //            throw new ValidationException("Check home drive syntax");
            //        else if (!regValidate.IsMatch(txt_homeDir.Text))
            //            throw new ValidationException("Check home directory syntax");
            //        else if (!regValidate.IsMatch(txt_city.Text))
            //            throw new ValidationException("Check city syntax");
            //        else if (!regValidate.IsMatch(txt_country.Text))
            //            throw new ValidationException("Check country syntax");
            //        else if (!regValidate.IsMatch(txt_description.Text))
            //            throw new ValidationException("Check description syntax");
            //    }
            //}
            //catch (ValidationException ve)
            //{
            //    Data_Access.Common.ReportError(ve);
            //    return false;
            //}

            return true;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

    class ValidationException : ApplicationException
    {
        public ValidationException(string message)
            : base(message)
        {
        }
    }
}